//
//  ProductInfoViewController.m
//  Finance
//
//  Created by cheng xiang on 13-4-6.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "ProductInfoViewController.h"
#import "AppDelegate.h"

@interface ProductInfoViewController ()

@end

@implementation ProductInfoViewController
@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:_tableView.bounds];
    backgroundView.image = [UIImage imageNamed:@"beij.png"];
    [_tableView setBackgroundView:backgroundView];
    
    [self.view addSubview:_tableView];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(goReturn)];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
    [delegate hideTabBar:aController];
}

- (void)viewDidDisappear:(BOOL)animated
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
    [delegate showTabBar:aController];
}

- (void)goReturn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *appVersion = delegate.app_version;
    NSString *autoRefresh = delegate.autoRefresh;
    NSString *sendNotification = delegate.sendNotification;
    NSString *identifier = [NSString stringWithFormat:@"%d at %d",indexPath.row,indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        switch (indexPath.section) {
            case 0:
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                if (indexPath.row == 0) {
                    cell.textLabel.text = [NSString stringWithFormat:@"现在版本：%@",appVersion];
                }else{
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_6_0
                    cell.textLabel.textAlignment = UITextAlignmentCenter;
#else
                    cell.textLabel.textAlignment = NSTextAlignmentCenter;
#endif
                    cell.textLabel.text = @"现在检测是否有更新";
                }
                break;
            case 1:
                cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                ((SettingCell *)cell).delegate = self;
                ((SettingCell *)cell).titleLabel.text = @"是否开启自动更新";
                ((SettingCell *)cell).section = indexPath.section;
                if ([autoRefresh isEqualToString:APP_TRUE]) {
                    [((SettingCell *)cell).selection setOn:true];
                }else{
                    [((SettingCell *)cell).selection setOn:false];
                }
                break;
            case 2:
                cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                ((SettingCell *)cell).delegate = self;
                ((SettingCell *)cell).titleLabel.text = @"是否开启信息推送";
                ((SettingCell *)cell).section = indexPath.section;
                if ([sendNotification isEqualToString:APP_TRUE]) {
                    [((SettingCell *)cell).selection setOn:true];
                }else{
                    [((SettingCell *)cell).selection setOn:false];
                }
                break;
            default:
                
                break;
        }
    }
    if (indexPath.row == 1 && indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }else{
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)clickOnSwitch:(BOOL)on atSection:(int)section
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    switch (section) {
        case 1:
            delegate.autoRefresh = on ? APP_TRUE : APP_FALSE;
            break;
        case 2:
            delegate.sendNotification = on ? APP_TRUE : APP_FALSE;
            break;
        default:
            break;
    }
    [self writeToFile:on ? APP_TRUE : APP_FALSE forKey:section == 1 ? @"autoRefresh" : @"sendNotification"];
}

//写入plist
- (void)writeToFile: (NSString *)value forKey:(NSString *)key
{
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:@"AppInfo.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    dic[key] = value;
    [dic writeToFile:filePath atomically:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = @"";
    switch (section) {
        case 0:
            title = @"软件版本";
            break;
        case 1:
            title = @"自动更新";
            break;
        case 2:
            title = @"信息推送";
            break;
        default:
            break;
    }
    return title;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 1 && indexPath.section == 0) {
        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [appDelegate onCheckVersion];
    }
}

@end
